﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using TimeTracker_Entities;
using TimeTracker_BL.Repositories;

namespace TimeTracker.Controllers
{
    [Authorize(Users = "dboss")]
    public class LookupsController : Controller
    {
        private ILookupsRepository _lookups;

        /// <summary>
        /// Initializes a new instance of the <see cref="LookupsController"/> class.
        /// </summary>
        public LookupsController()
        {
            _lookups = new LookupsRepository();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LookupsController"/> class.
        /// </summary>
        /// <param name="timeTrackerDb">The time tracker db.</param>
        /// <param name="lookupsDb">The lookups db.</param>
        public LookupsController(ILookupsRepository lookupsDb)
        {
            _lookups = lookupsDb;
        }

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Gets the billing terms.
        /// </summary>
        [GridAction]
        public ActionResult _GetBillingTerms()
        {
            // Get the Billing terms from the database
            IList<BillingTerm> terms = _lookups.GetBillingTerms();

            return View(new GridModel<BillingTerm>(terms));
        }

        /// <summary>
        /// Adds the billing term.
        /// </summary>
        /// <param name="addedTerm">The added term.</param>
        [GridAction]
        public ActionResult _AddBillingTerm(BillingTerm addedTerm)
        {
            // Add the term to the database
            var result = _lookups.AddBillingTerm(addedTerm);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetBillingTerms();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        /// <summary>
        /// Updates the billing term.
        /// </summary>
        /// <param name="updatedTerm">The updated term.</param>
        [GridAction]
        public ActionResult _UpdateBillingTerm(BillingTerm updatedTerm)
        {
            // Update the term
            var result = _lookups.UpdateBillingTerm(updatedTerm);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetBillingTerms();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }

        /// <summary>
        /// Deletes the billing term.
        /// </summary>
        /// <param name="deletedTerm">The deleted term.</param>
        [GridAction]
        public ActionResult _DeleteBillingTerm(int id)
        {
            // Delete the term
            var result = _lookups.DeleteBillingTerm(id);

            // Return either the updated list or an error message
            if (result.IsSuccessful)
            {
                return _GetBillingTerms();
            }
            else
            {
                return Content(result.ErrorMessage);
            }
        }
    }
}
